package com.example.demo.bizhan.thread;

/**
 * @description: 主线程中获取子线程返回值
 * @time: 2020-10-27 20:44
 * @author: baojinlong
 **/
public class ThreadDemo03 implements Runnable {

    private String value;

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        value = "我是子线程的返回值";
    }

    public static void main(String[] args) throws Exception {
        ThreadDemo03 threadDemo03 = new ThreadDemo03();
        Thread thread = new Thread(threadDemo03);
        thread.start();

        // 方法一
//        while (threadDemo03.value == null) {
//            Thread.sleep(1000);
//        }
        // 方法二 使用线程的join来获取子线程的返回值 Waits for this thread to die.
        thread.join();
        // 方法三 使用callable方式

        System.out.println("返回值" + threadDemo03.value);
    }
}
